﻿<%@ WebHandler Language="C#" Class="ScheduledTaskHandler" %>
//Description:定时任务维护
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:Wangjh
//create Date:2022-08-07
using System;
using System.Web;
using BusinessRule.DTO;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using BusinessRule.Business;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data;
using System.Linq;


public class ScheduledTaskHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    ScheduledTaskBusiness m_stBusiness = new ScheduledTaskBusiness();
    private LimitedCapacityBusiness m_LMObj = new LimitedCapacityBusiness();
    private uLMSCommonIBusiness m_CommonObj = uMESBusinessFactory.instance.GetuLMSCommonObject();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            if (m_UserInfo == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "登录过期" }));
                return;
            }
            switch (action)
            {
                case "getPoolList"://获取排产池列表  
                    GetPoolList(context);
                    break;
                case "searchSTData":
                    SearchSTData(context);
                    break;
                case "saveApsScheduledTask":
                    SaveApsScheduledTask(context);
                    break;
                case "deleteApsScheduledTask":
                    DeleteApsScheduledTask(context);
                    break;
                case "loadScheduledTaskLog":
                    LoadScheduledTaskLog(context);
                    break;
                case "manualScheduledTask":
                    ManualScheduledTask(context);
                    break;
                case "GetScheduledTaskDetailData"://获取定时任务明细 add by zhangrj 20231027
                    GetScheduledTaskDetailData(context);
                    break;
                case "GetScheduledTaskOperaLog"://获取操作日志 add by zhangrj 20231219
                    GetScheduledTaskOperaLog(context);
                    break;
                case "SaveScheduledTaskData"://保存定时任务数据（通用保存） add by zhangrj 20240719
                    SaveScheduledTaskData(context);
                    break;
                case "saveManualNextDate"://手动修改下次执行时间
                    saveManualNextDate(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception e)
        {
            var result = new uMESProcessResult()
            {
                Result = false,
                Message = e.Message
            };
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }
    /// <summary>
    /// 保存下次执行时间
    /// add:wangjh 20240827
    /// </summary>
    /// <param name="context"></param>
    public void saveManualNextDate(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["ScheduledTaskId"] = context.Request["scheduledTaskId"].ToString();
        para["nextExcuTime"] = context.Request["nextExcuTime"].ToString();

        var exuteEntity = new ExcuteEntity("scheduledtask", ExcuteEntity.ExcuteTypeEnum.update);
        var excuteFileds = new List<FieldEntity>() { new FieldEntity("nextExcuTime", DateTime.Parse(para["nextExcuTime"]), FieldEntity.FieldTypeEnum.DateTime) };
        exuteEntity.ExcuteFileds = excuteFileds;
        exuteEntity.WhereFileds = new List<FieldEntity>() { new FieldEntity("ScheduledTaskId", para["ScheduledTaskId"], FieldEntity.FieldTypeEnum.Str) };
        DatabaseHelper.ExecuteDataByEntity(exuteEntity);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 保存定时任务数据（通用保存） add by zhangrj 20240719
    /// </summary>
    /// <param name="context"></param>
    public void SaveScheduledTaskData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["EmployeeID"] = m_UserInfo.EmployeeID.ToString();
        para["FactoryID"] = m_UserInfo.FactoryID.ToString();
        para["ScheduledTaskId"] = context.Request["scheduledTaskId"].ToString();
        para["TaskName"] = context.Request["taskName"].ToString();
        para["TaskStartTime"] = context.Request["taskStartDate"].ToString();
        para["TaskEndTime"] = context.Request["taskEndDate"].ToString();
        para["TaskDesc"] = context.Request["taskDesc"].ToString();
        para["Cycletime"] = context.Request["cycleTime"].ToString();
        para["TaskType"] = context.Request["taskType"].ToString();

        DataTable attrDt = new DataTable();//任务选项保存
        attrDt.Columns.AddRange(new DataColumn[] { 
            new DataColumn("ATTRIBUTENAME"),new DataColumn("ATTRIBUTEVALUE"),new DataColumn("IsUse")
        });
        DataRow attrRow;
        TaskTypeEnum taskType = (TaskTypeEnum)int.Parse(para["TaskType"]);
        /*----------验证并添加属性-----------*/
        switch (taskType)
        {
            case TaskTypeEnum.自动排产://原先就有单独保存方法，暂未移至此处
                break;
            case TaskTypeEnum.自动开卡://暂无任务选项配置保存
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                break;
            case TaskTypeEnum.热表同步现场台账:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "库房范围";
                attrRow["ATTRIBUTEVALUE"] = context.Request["roomRange"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                break;
            case TaskTypeEnum.热表预排制造计划:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "排产池";
                attrRow["ATTRIBUTEVALUE"] = context.Request["preApsPool"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "预排覆盖周期";
                attrRow["ATTRIBUTEVALUE"] = context.Request["preApsCoverCycle"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "连续加工间隔时间";
                attrRow["ATTRIBUTEVALUE"] = context.Request["continuityWorkTimeNum"].ToString();
                attrRow["IsUse"] = 0;
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "预排参考";
                attrRow["ATTRIBUTEVALUE"] = context.Request["preApsRefer"].ToString();
                attrRow["IsUse"] = 0;
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "预警规则";
                attrRow["ATTRIBUTEVALUE"] = context.Request["preWarningRule"].ToString();
                attrRow["IsUse"] = 0;
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "预警检查";
                attrRow["ATTRIBUTEVALUE"] = context.Request["preWarningCheck"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                break;
            case TaskTypeEnum.热表生产准备检查://暂无任务选项配置保存
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                break;
            case TaskTypeEnum.热表转工计划接收:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "接收参考";
                attrRow["ATTRIBUTEVALUE"] = context.Request["receiveRefer"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "接收后自动开卡";
                attrRow["ATTRIBUTEVALUE"] = context.Request["revedStart"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "覆盖周期";
                attrRow["ATTRIBUTEVALUE"] = context.Request["revCoverCycle"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                break;
            case TaskTypeEnum.热表滚动制造计划:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "覆盖周期";
                attrRow["ATTRIBUTEVALUE"] = context.Request["apsCoverCycle"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "连续加工间隔时间";
                attrRow["ATTRIBUTEVALUE"] = context.Request["continuityWorkTimeNum"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                break;
            case TaskTypeEnum.自动预排产:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType, "排产池", context.Request["apspoolid"].ToString())) //添加验证
                {
                    result.Result = false; result.Message = "存在相同排产池的任务";
                    break;
                }
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "排产池";
                attrRow["ATTRIBUTEVALUE"] = context.Request["apspoolid"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                //attrRow = attrDt.NewRow();
                //attrRow["ATTRIBUTENAME"] = "排到人";
                //attrRow["ATTRIBUTEVALUE"] = context.Request["isApsToEmp"].ToString();
                //attrRow["IsUse"] = 0;
                //attrDt.Rows.Add(attrRow);
                break;
            case TaskTypeEnum.自动转工排产接收:
                if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]) && IsExistScheduledInfo(taskType)) //添加验证
                {
                    result.Result = false; result.Message = "已存在相同的定时任务，无需重复添加";
                    break;
                }
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "接收参考";
                attrRow["ATTRIBUTEVALUE"] = context.Request["receiveRefer"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                /*---------------------*/
                attrRow = attrDt.NewRow();
                attrRow["ATTRIBUTENAME"] = "覆盖周期";
                attrRow["ATTRIBUTEVALUE"] = context.Request["revCoverCycle"].ToString();
                attrRow["IsUse"] = 0;
                attrDt.Rows.Add(attrRow);
                break;
            default:
                break;
        }
        if (result.Result)
        {
            result = m_stBusiness.SaveScheduledTask(para, attrDt);
        }
        result.Message = result.Result ? "保存成功" : result.Message;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 判断是否存在相同的任务或相同任务属性值
    /// </summary>
    /// <param name="taskType">任务类型</param>
    /// <param name="attrName">属性名</param>
    /// <param name="attrValue">属性值</param>
    /// <returns></returns>
    private bool IsExistScheduledInfo(TaskTypeEnum taskType, string attrName = "", string attrValue = "")
    {
        string sql = string.Format(@"SELECT 1 FROM ScheduledTask s WHERE s.tasktype = {0} AND s.factoryid = '{1}' AND s.isuse = 0", int.Parse(((Enum)taskType).ToString("D")), m_UserInfo.FactoryID);
        if (!string.IsNullOrWhiteSpace(attrName))
        {
            sql = string.Format(@"SELECT 1 FROM ScheduledTask s 
                                      INNER JOIN SCHEDULEDTASKATTR sa ON sa.SCHEDULEDTASKID = s.SCHEDULEDTASKID AND sa.ISUSE=0 AND sa.ATTRIBUTENAME = '{2}' AND sa.attributevalue = '{3}'
                                      WHERE s.tasktype = {0} AND s.factoryid = '{1}' AND s.isuse = 0 ", int.Parse(((Enum)taskType).ToString("D")), m_UserInfo.FactoryID, attrName, attrValue);
        }
        if (DatabaseHelper.GetSingle(sql) != null)
        {
            return true;
        }
        return false;
    }


    /// <summary>
    /// 获取操作日志
    /// </summary>
    /// <param name="context"></param>
    public void GetScheduledTaskOperaLog(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        string factoryId = m_UserInfo.FactoryID;
        if (context.Request.Form.AllKeys.Contains("factoryId"))
        {
            factoryId = context.Request["factoryId"];
        }
        string sql = string.Format(@"SELECT s.taskname,s.tasktype,cp.operationdate,cp.notes,e.fullname
                        FROM CommonMethodOperaLog cp 
                        INNER JOIN scheduledtask s ON s.scheduledtaskid = cp.parentid AND s.factoryid = '{0}'
                        LEFT JOIN employee e ON e.employeeid = cp.operationemployeeid
                        ORDER BY cp.operationdate DESC", factoryId);
        result.DBTable = DatabaseHelper.Query_ReturnDatTable(sql);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 定时任务类型 （需要增加定时任务的往后添加） add by zhangrj 20240929
    /// </summary>
    public enum TaskTypeEnum
    {
        自动排产 = 1, 自动开卡 = 2, 热表同步现场台账 = 3,
        热表预排制造计划 = 4,
        热表生产准备检查 = 5,
        热表转工计划接收 = 6,
        热表滚动制造计划 = 7,
        自动预排产 = 8,
        自动转工排产接收 = 9
    }
    /// <summary>
    /// 获取任务明细
    /// </summary>
    /// <param name="context"></param>
    public void GetScheduledTaskDetailData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        TaskTypeEnum taskType = (TaskTypeEnum)int.Parse(context.Request["taskType"].ToString());
        string scheduledTaskId = context.Request["scheduledTaskId"].ToString();
        string sql = string.Empty;
        switch (taskType)
        {
            case TaskTypeEnum.自动排产:
                sql = @"SELECT SA.ATTRIBUTEVALUE ISCALPRIORITY,SA2.ATTRIBUTEVALUE APSPOOLID,SA3.ATTRIBUTEVALUE ISAPSTOEMP 
                        FROM SCHEDULEDTASK ST
                        LEFT JOIN SCHEDULEDTASKATTR SA ON SA.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA.ISUSE=0 AND SA.ATTRIBUTENAME=ST.TASKTYPE||'重算优先级'
                        LEFT JOIN SCHEDULEDTASKATTR SA2 ON SA2.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA2.ISUSE=0 AND SA2.ATTRIBUTENAME=ST.TASKTYPE||'排产池'
                        LEFT JOIN SCHEDULEDTASKATTR SA3 ON SA3.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA3.ISUSE=0 AND SA3.ATTRIBUTENAME=ST.TASKTYPE||'排到人'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.自动开卡:
                break;
            case TaskTypeEnum.热表同步现场台账:
                sql = @"SELECT sa.attributevalue roomrange
                        FROM scheduledtask st
                        LEFT JOIN SCHEDULEDTASKATTR sa ON sa.SCHEDULEDTASKID = st.scheduledtaskid AND sa.ISUSE=0 AND sa.ATTRIBUTENAME= '库房范围'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.热表预排制造计划:
                sql = @"SELECT sa1.attributevalue ApsPool,sa2.attributevalue ApsCoverCycle,sa3.attributevalue WorkTimeNum,sa4.attributevalue ApsRefer,
                        sa5.attributevalue WarningRule,sa6.attributevalue WarningCheck
                        FROM scheduledtask st
                        LEFT JOIN SCHEDULEDTASKATTR sa1 ON sa1.SCHEDULEDTASKID = st.scheduledtaskid AND sa1.ISUSE=0 AND sa1.ATTRIBUTENAME= '排产池'
                        LEFT JOIN SCHEDULEDTASKATTR SA2 ON SA2.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA2.ISUSE=0 AND SA2.ATTRIBUTENAME='预排覆盖周期'
                        LEFT JOIN SCHEDULEDTASKATTR SA3 ON SA3.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA3.ISUSE=0 AND SA3.ATTRIBUTENAME='连续加工间隔时间'
                        LEFT JOIN SCHEDULEDTASKATTR SA4 ON SA4.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA4.ISUSE=0 AND SA4.ATTRIBUTENAME='预排参考'
                        LEFT JOIN SCHEDULEDTASKATTR SA5 ON SA5.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA5.ISUSE=0 AND SA5.ATTRIBUTENAME='预警规则'
                        LEFT JOIN SCHEDULEDTASKATTR SA6 ON SA6.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA6.ISUSE=0 AND SA6.ATTRIBUTENAME='预警检查'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.热表生产准备检查:
                break;
            case TaskTypeEnum.热表转工计划接收:
                sql = @"SELECT sa1.attributevalue ReceiveRefer,sa2.attributevalue RevedStart,sa3.attributevalue RevCoverCycle
                        FROM scheduledtask st
                        LEFT JOIN SCHEDULEDTASKATTR sa1 ON sa1.SCHEDULEDTASKID = st.scheduledtaskid AND sa1.ISUSE=0 AND sa1.ATTRIBUTENAME= '接收参考'
                        LEFT JOIN SCHEDULEDTASKATTR SA2 ON SA2.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA2.ISUSE=0 AND SA2.ATTRIBUTENAME='接收后自动开卡'
                        LEFT JOIN SCHEDULEDTASKATTR SA3 ON SA3.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA3.ISUSE=0 AND SA3.ATTRIBUTENAME='覆盖周期'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.热表滚动制造计划:
                sql = @"SELECT sa1.attributevalue CoverCycle,sa2.attributevalue WorkTimeNum
                        FROM scheduledtask st
                        LEFT JOIN SCHEDULEDTASKATTR sa1 ON sa1.SCHEDULEDTASKID = st.scheduledtaskid AND sa1.ISUSE=0 AND sa1.ATTRIBUTENAME= '覆盖周期'
                        LEFT JOIN SCHEDULEDTASKATTR sa2 ON sa2.SCHEDULEDTASKID = st.scheduledtaskid AND sa2.ISUSE=0 AND sa2.ATTRIBUTENAME= '连续加工间隔时间'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.自动预排产:
                sql = @"SELECT SA2.ATTRIBUTEVALUE APSPOOLID,SA3.ATTRIBUTEVALUE ISAPSTOEMP 
                        FROM SCHEDULEDTASK ST
                        LEFT JOIN SCHEDULEDTASKATTR SA2 ON SA2.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA2.ISUSE=0 AND SA2.ATTRIBUTENAME = '排产池'
                        LEFT JOIN SCHEDULEDTASKATTR SA3 ON SA3.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND SA3.ISUSE=0 AND SA3.ATTRIBUTENAME = '排到人'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            case TaskTypeEnum.自动转工排产接收:
                sql = @"SELECT sa1.attributevalue ReceiveRefer,sa2.attributevalue RevCoverCycle
                        FROM scheduledtask st
                        LEFT JOIN SCHEDULEDTASKATTR sa1 ON sa1.SCHEDULEDTASKID = st.scheduledtaskid AND sa1.ISUSE=0 AND sa1.ATTRIBUTENAME= '接收参考'
                        LEFT JOIN SCHEDULEDTASKATTR sa2 ON sa2.SCHEDULEDTASKID=ST.SCHEDULEDTASKID AND sa2.ISUSE=0 AND sa2.ATTRIBUTENAME='覆盖周期'
                        WHERE st.scheduledtaskid = '{0}'";
                break;
            default:
                break;
        }
        if (!string.IsNullOrWhiteSpace(sql))
        {
            sql = string.Format(sql, scheduledTaskId);
            result.DBTable = DatabaseHelper.Query_ReturnDatTable(sql);
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 验证是否存在相同的属性
    /// </summary>
    /// <param name="taskType">任务类型</param>
    /// <param name="attrName">属性名</param>
    /// <param name="attrValue">属性值</param>
    /// <param name="isPrecise">是否精准查询</param>
    /// <returns></returns>
    private bool IsExistsSameAttr(string taskType, string attrName, string attrValue, bool isPrecise = true)
    {
        string sql = string.Format(@"SELECT 1 FROM scheduledtask s
                        LEFT JOIN SCHEDULEDTASKATTR sc ON sc.scheduledtaskid = s.scheduledtaskid
                        WHERE s.tasktype = {0} AND sc.isuse = 0 AND sc.attributename = '{1}' AND sc.attributevalue {2}", int.Parse(taskType), attrName, isPrecise ? string.Format("= '{0}'", attrValue) : string.Format("like '%{0}%'", attrValue));
        if (DatabaseHelper.GetSingle(sql) == null)
        {
            return false;
        }
        return true;
    }

    public void GetPoolList(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["FactoryID"] = context.Request["factoryId"].ToString();

        DataTable dt = m_LMObj.GetPoolList(para);

        context.Response.Write(JsonConvert.SerializeObject(dt));
    }

    /// <summary>
    /// 查询获取列表
    /// </summary>
    /// <param name="context"></param>
    public void SearchSTData(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        Dictionary<string, string> para = new Dictionary<string, string>();
        if (!context.Request.Form.AllKeys.Contains("factoryId"))
        {
            para.Add("FactoryID", m_UserInfo.FactoryID);
        }
        else
        {
            para.Add("FactoryID", context.Request["factoryId"].ToString());
        }

        para.Add("CurrentPageIndex", context.Request["pageNumber"].ToString());//页码
        para.Add("PageSize", context.Request["pageSize"].ToString());//每页页数

        result = m_stBusiness.GetScheduledTaskData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 保存定时任务信息
    /// </summary>
    /// <param name="context"></param>
    public void SaveApsScheduledTask(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        Dictionary<string, string> para = new Dictionary<string, string>();
        var apspoolid = context.Request["apspoolid"].ToString();
        para["EmployeeID"] = m_UserInfo.EmployeeID.ToString();
        para["FactoryID"] = context.Request["factoryId"].ToString();
        para["ScheduledTaskId"] = context.Request["scheduledtaskid"].ToString();
        para["TaskName"] = context.Request["taskname"].ToString();
        para["TaskStartTime"] = context.Request["taskstarttime"].ToString();
        para["TaskEndTime"] = context.Request["taskendtime"].ToString();
        para["TaskDesc"] = context.Request["taskdesc"].ToString();
        para["Cycletime"] = context.Request["cycletime"].ToString();
        para["TaskType"] = context.Request["taskType"].ToString();

        DataTable attrDt = new DataTable();
        attrDt.Columns.AddRange(new DataColumn[] { 
            new DataColumn("ATTRIBUTENAME"),new DataColumn("ATTRIBUTEVALUE"),new DataColumn("IsUse")
        });
        var attrRow = attrDt.NewRow();
        attrRow["ATTRIBUTENAME"] = para["TaskType"] + "排产池";
        attrRow["ATTRIBUTEVALUE"] = apspoolid;
        attrRow["IsUse"] = 0;
        attrDt.Rows.Add(attrRow);

        attrRow = attrDt.NewRow();
        attrRow["ATTRIBUTENAME"] = para["TaskType"] + "重算优先级";
        attrRow["ATTRIBUTEVALUE"] = context.Request["isCalPriority"].ToString();
        attrRow["IsUse"] = 0;
        attrDt.Rows.Add(attrRow);

        attrRow = attrDt.NewRow();
        attrRow["ATTRIBUTENAME"] = para["TaskType"] + "排到人";
        attrRow["ATTRIBUTEVALUE"] = context.Request["isApsToEmp"].ToString();
        attrRow["IsUse"] = 0;
        attrDt.Rows.Add(attrRow);

        if (string.IsNullOrWhiteSpace(para["ScheduledTaskId"]))
        {
            if (DatabaseHelper.QueryDataByEntity(new ExcuteEntity("SCHEDULEDTASKATTR", ExcuteEntity.ExcuteTypeEnum.selectAll)
            {
                WhereFileds = new List<FieldEntity>() { new FieldEntity("ATTRIBUTEVALUE", apspoolid, FieldEntity.FieldTypeEnum.Str), new FieldEntity("isuse", 0, FieldEntity.FieldTypeEnum.Numer),
                new FieldEntity("ATTRIBUTENAME", para["TaskType"] +"排产池", FieldEntity.FieldTypeEnum.Str)}
            }).Rows.Count > 0)
            {
                result.Message = "此排产池已有定时任务";
                context.Response.Write(JsonConvert.SerializeObject(result));
                return;
            }
        }

        result = m_stBusiness.SaveScheduledTask(para, attrDt);

        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 删除定时任务信息
    /// </summary>
    /// <param name="context"></param>
    public void DeleteApsScheduledTask(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        var id = context.Request["scheduledtaskid"].ToString();
        var listExuteEntity = new List<ExcuteEntity>();

        listExuteEntity.Add(new ExcuteEntity("ScheduledTask", ExcuteEntity.ExcuteTypeEnum.update)
        {
            ExcuteFileds = new List<FieldEntity>() { new FieldEntity("isuse", 1, FieldEntity.FieldTypeEnum.Numer) },
            WhereFileds = new List<FieldEntity>() { new FieldEntity("ScheduledTaskID", id, FieldEntity.FieldTypeEnum.Str) }
        });
        listExuteEntity.Add(new ExcuteEntity("scheduledtaskattr", ExcuteEntity.ExcuteTypeEnum.update)
        {
            ExcuteFileds = new List<FieldEntity>() { new FieldEntity("isuse", 1, FieldEntity.FieldTypeEnum.Numer) },
            WhereFileds = new List<FieldEntity>() { new FieldEntity("ScheduledTaskID", id, FieldEntity.FieldTypeEnum.Str) }
        });
        DatabaseHelper.ExecuteDataByEntitys(listExuteEntity);

        m_CommonObj.InsertCommonMethodOperaLogInfo(id, m_UserInfo.EmployeeID, "删除定时任务", "ScheduledTaskDele");//新增删除记录 add by zhangrj 20231219

        result.Result = true;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 加载定时任务日志
    /// </summary>
    /// <param name="context"></param>
    public void LoadScheduledTaskLog(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        var id = context.Request["scheduledtaskid"].ToString();

        var dt = m_stBusiness.GetScheduledTaskLog(id);

        result.Result = true;
        result.DBTable = dt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 手动开始或停止任务
    /// </summary>
    /// <param name="context"></param>
    public void ManualScheduledTask(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        var para = new Dictionary<string, string>();
        para["ScheduledTaskId"] = context.Request["scheduledtaskid"].ToString();
        para["Manualtype"] = context.Request["manualtype"].ToString();
        para["ManualOperEmp"] = m_UserInfo.FullName;
        result = m_stBusiness.ManualScheduledTask(para);

        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}